package com.toe.recommend.webapi.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.toe.recommend.webapi.pojo.LngLat;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class LocationUtil {

    private final static String TextURL = "https://restapi.amap.com/v3/place/text?";
    private final static String AroundURL = "https://restapi.amap.com/v3/place/around?";
    private final static String GeoURL = "https://restapi.amap.com/v3/geocode/geo?";
    private final static String ReGeoURL = "https://restapi.amap.com/v3/geocode/regeo?";
    private final static String KEY = "2c5f304afe331c3243cd4f2f3c0363b6";

    /**
     * 根据自身位置获取周边50000m范围内的健身房
     * @param location 自身当前位置
     * @return 符合条件的健身房的id
     */
    public static List<String> getAround(String location) {
        StringBuffer str = new StringBuffer(AroundURL);
        str.append("location=").append(location)
                .append("&sortrule=distance")
                .append("&keywords=健身房")
                .append("&key=").append(KEY)
                .append("&output=json")
                .append("&extensions=all");
        String addressMessage = HttpUtil.get(str.toString()).toString();
        JSONObject result = JSONObject.parseObject(addressMessage);
        String status = result.getString("status");

        if ((status.equals("1"))) {
            JSONArray pois = result.getJSONArray("pois");
            List<String> list = new ArrayList<>();
            for (Object o : pois) {
                JSONObject jsonObject = (JSONObject) o;
                String id = jsonObject.getString("id");
                list.add(id);
            }
            return list;
        }
        return null;
    }

    /**
     * 根据用户的起点和终点经纬度计算两点间距离，此距离为相对较短的距离，单位米。
     *
     * @param start 起点的坐标
     * @param end   终点的坐标
     * @return 两点的距离
     */
    public static double calculateLineDistance(LngLat start, LngLat end) {
        if ((start == null) || (end == null)) {
            throw new IllegalArgumentException("非法坐标值，不能为null");
        }
        double d1 = 0.01745329251994329D;
        double d2 = start.longitude;
        double d3 = start.latitude;
        double d4 = end.longitude;
        double d5 = end.latitude;
        d2 *= d1;
        d3 *= d1;
        d4 *= d1;
        d5 *= d1;
        double d6 = Math.sin(d2);
        double d7 = Math.sin(d3);
        double d8 = Math.cos(d2);
        double d9 = Math.cos(d3);
        double d10 = Math.sin(d4);
        double d11 = Math.sin(d5);
        double d12 = Math.cos(d4);
        double d13 = Math.cos(d5);
        double[] arrayOfDouble1 = new double[3];
        double[] arrayOfDouble2 = new double[3];
        arrayOfDouble1[0] = (d9 * d8);
        arrayOfDouble1[1] = (d9 * d6);
        arrayOfDouble1[2] = d7;
        arrayOfDouble2[0] = (d13 * d12);
        arrayOfDouble2[1] = (d13 * d10);
        arrayOfDouble2[2] = d11;
        double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
                + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
                + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));

        return (Math.asin(d14 / 2.0D) * 12742001.579854401D);
    }

}
